package team.knowers.qzhserver.course.course.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.knowers.qzhserver.common.api.CommonResult;
import team.knowers.qzhserver.course.course.entity.dao.CourseDO;
import team.knowers.qzhserver.course.course.entity.dto.CourseReleaseDTO;
import team.knowers.qzhserver.course.course.entity.dto.CourseUpdateDTO;
import team.knowers.qzhserver.course.course.service.CourseService;
import team.knowers.qzhserver.user.user.service.UserService;

import java.util.Objects;

/**
 * @author zcb
 * @date 2024/3/6 14:00
 */
@Controller
@Tag(name = "CourseController", description = "课程信息操作")
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserService userService;

    @Operation(summary = "创建一个空白的新课程")
    @PostMapping("/create")
    @ResponseBody
    public CommonResult createCourse(@RequestParam String uid,@RequestParam String name){
        //防止参数不合理
        if(userService.userStatus(uid)==-1)
            return CommonResult.failed("用户不存在");
        CourseDO courseDO = courseService.createCourse(uid,name);
        if(courseDO!=null){
            return CommonResult.success(courseDO,uid+"成功创建课程"+name);
        }else {
            return CommonResult.failed("创建课程失败");
        }
    }
    @Operation(summary = "更新课程信息")
    @PutMapping("/update")
    @ResponseBody
    public CommonResult updateCourse(@RequestBody CourseUpdateDTO courseUpdateDTO){
        //防止参数不合理
        if(userService.userStatus(courseUpdateDTO.getUid())==-1)
            return CommonResult.failed("用户不存在");
        CourseDO courseDO = courseService.getCourseById(courseUpdateDTO.getId());
        if(courseDO == null)
            return CommonResult.failed("课程不存在");
        if(!(Objects.equals(courseUpdateDTO.getUid(), courseDO.getUserId())))
            return CommonResult.failed("该课程不属于该用户");
        if(courseService.updateCourse(courseUpdateDTO)){
            return CommonResult.success(null,"课程信息更新成功");
        }else {
            return CommonResult.failed("课程信息更新失败");
        }
    }
    @Operation(summary = "删除课程信息")
    @DeleteMapping("delete")
    @ResponseBody
    public CommonResult deleteCourse(@RequestParam String id,@RequestParam String uid) throws Exception {
        //防止参数不合理
        if(userService.userStatus(uid)==-1)
            return CommonResult.failed("用户不存在");
        CourseDO courseDO = courseService.getCourseById(id);
        if(courseDO == null)
            return CommonResult.failed("课程不存在");
        if(!(Objects.equals(uid, courseDO.getUserId())))
            return CommonResult.failed("该课程不属于该用户");
        if(courseService.deleteCourse(id)){
            return CommonResult.success(null,"成功删除课程");
        }else{
            return CommonResult.failed("删除课程失败");
        }
        //TODO 课程关联的其他信息的同时删除
    }
    @Operation(summary = "发布课程")
    @PutMapping("/release")
    @ResponseBody
    public CommonResult releaseCourse(@RequestBody CourseReleaseDTO courseReleaseDTO){
        //防止参数不合理
        if(userService.userStatus(courseReleaseDTO.getUid())==-1)
            return CommonResult.failed("用户不存在");
        CourseDO courseDO = courseService.getCourseById(courseReleaseDTO.getId());
        if(courseDO == null)
            return CommonResult.failed("课程不存在");
        if(!(Objects.equals(courseReleaseDTO.getUid(), courseDO.getUserId())))
            return CommonResult.failed("该课程不属于该用户");
        if (courseService.releaseCourse(courseReleaseDTO)){
            return CommonResult.success(null,"课程发布成功，等待审核中");
        }else{
            return CommonResult.failed("课程发布失败");
        }
    }
}
